Page({
    data: {
        cartList: [{ id: 1, goods_price: 28, num: 1, text: '1212' }, { id: 2, goods_price: 33, num: 2, text: '124212' }],
        allChecked: false, //全选
        totalPrice: 0, //选中项价格总计
        // checked: false,
    },
    //购物车商品复选框的change事件
    handleItemChange(e) {
        const goods_id = e.currentTarget.dataset.id
        const { cartList } = this.data
        const checkIndex = cartList.findIndex(v => v.id === goods_id)
        cartList[checkIndex].checked = !cartList[checkIndex].checked
        this.setCart(cartList)
    },
    //全选全不选
    handleItemAllchecked() {
        let { cartList, allChecked } = this.data
        allChecked = !allChecked
        cartList.forEach(v => v.checked = allChecked)
        this.setCart(cartList)
    },
    //公用部分 设置购物车状态的同时改变底部工具栏的数据
    setCart(cart) {
        let allChecked = true
        let totalPrice = 0
        cart.forEach(v => {
            if (v.checked) {
                totalPrice += v.goods_price * v.num * 100
            } else {
                allChecked = false
            }
        })
        allChecked = cart.length === 0 ? false : allChecked
        this.setData({
            cartList: cart,
            allChecked,
            totalPrice
        })
    },
    onChange(event) {
        console.log(event.detail);
    },
    /*点击结算*/
    onClickButton() {
        wx.navigateTo({
            url: '/pages/order_confirm/order_confirm',
        })
    }
})